package com.kinderas.gl
{
	public class Color{
		
		/**
		 * Takes a pixel color(RGB) value and converts it into into an Array containing individual R,G,B vaulues.<br />
		 * <strong>Example</strong><br />
		 * This example samples the pixel at position 10,10 of the bitmapdata object,
		 * and returns an Array of the induvidual R,G,B values.
		 * <listing>
		 * var pixel:uint = myBitmapData.getPixel(10,10);
		 * var RGBArray:Array = pixelToRGB(pixel);
		 * </listing> 
		 * @param pixelColor
		 * @return
		 * 
		 */		
		public static function pixelToRGB(pixelColor:uint):Array {
			var red:Number   = pixelColor >> 16 & 0xFF;
			var green:Number = pixelColor >> 8 & 0xFF;
			var blue:Number  = pixelColor & 0xFF;
			var ar:Array = new Array(red,green,blue);
			return ar;
		}
		
		/**
		 * This function takes three params, red, green and blue color values and 
		 * returns the corresponding pixel value in an unmultiplied RGB format.
		 * @param r Red value
		 * @param g Green value
		 * @param b Blue value
		 * @return Pixel color value as an unsigned integer
		 * 
		 */	    
		public static function RGBToPixel(r:uint,g:uint,b:uint):uint{
			var hex:uint = r << 16 ^ g << 8 ^ b;
		    return hex;
		}
		
		/**
		 * Takes induvidual r,g,b color values and returns the corresponding H,S,L values
		 * in an Array(h,s,l). <br />
		 * <strong>Note:</strong> This function only handle the 16bit color values, to perserve 
		 * any alpha(32bit) values, add it later by storing it locally.
		 * @param red Red value
		 * @param grn Green value
		 * @param blu Blue value
		 * @return Array of HUE, Saturation, Luminace
		 * 
		 */		
		public static function RGBToHSL(red:uint, grn:uint, blu:uint):Array {
			var hue:Number;
			var sat:Number;
			var lum:Number;
		
			var max:Number = Math.max(red,grn,blu);
			var min:Number = Math.min(red,grn,blu);
		
			var delt:Number = max - min;
		
			lum = max/255;//get brightness
			sat = (max != 0) ? delt / max : 0;//Get saturation
		
			//hue
			if (sat == 0) {
				hue = 0;
			} else {
				var rr:Number = (max - red) / delt;
				var gr:Number = (max - grn) / delt;
				var br:Number = (max - blu) / delt;
				if (red == max) {
					hue = br - gr;
				} else if (grn == max) {
					hue = 2 + rr - br;
				} else {
					hue = 4 + gr - rr;
				}
				hue /= 6;
				if (hue < 0) {
					hue++;
				}
			}
			var output:Array = new Array(Math.round(hue*360), Math.round(sat*100), Math.round(lum*100));
			return output;
		}
		
		/**
		 * Takes the h,s,l vaules of a color and returns the corresponding r,g,b colors(16bit) 
		 * @param h HUE
		 * @param s Saturation
		 * @param l Luminace or brightness
		 * @return Array of RGB values
		 * 
		 */		
		public static function HSLToRGB(h:uint, s:uint, v:uint):Array{
			var br:Number = Math.round(v / 100 * 255);
			if(s == 0){
				return new Array(br,br,br);
			}else{
				var hue:Number = h % 360;
				var f:Number   = hue % 60;
				var p:Number   = Math.round((v * (100 - s)) / 10000 * 255);
				var q:Number   = Math.round((v * (6000 - s * f)) / 600000 * 255);
				var t:Number   = Math.round((v * (6000 - s * (60 - f))) / 600000 * 255);
				
				switch (Math.floor(hue / 60)){
					case 0: return new Array(br,t,p);
					case 1: return new Array(q,br,p);
					case 2: return new Array(p,br,t);
					case 3: return new Array(p,q,br);
					case 4: return new Array(t,p,br);
					case 5: return new Array(br,p,q);
				}
			}
			return new Array(0,0,0);
		}
	}
}